package dao.daoimpl;

import util.DataBase;
import dao.*;
import bean.*;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


public class ArticleDaoImpl implements ArticleDao {
    @Override/*包括插入博客信息，以及多标签到数据库中*/
    public boolean addArticle(Article article) throws SQLException {
        /*将博客信息插入到数据库中*/
        String content=article.getContent();
        int likingDegree=article.getLikingDegree();
        int views=article.getViews();
        String title=article.getTitle();
        String time = article.getTime();
        int commentCount=article.getCommentCount();
        int category=article.getCategory();
        String user=article.getUser();
        String type=article.getType();
        String subfield=article.getSubfield();
        int approve=article.getApprove();
        int like=article.getLike();
        String sql="insert into article_table (article_content,article_likingDegree,article_views,article_title,article_time,article_commentCount,article_categoryId,article_user,article_type,article_subfield,article_approve,article_like) values" +
                "('"+content+"','"+likingDegree+"','"+views+"','"+title+"','"+time+"','"+commentCount+"','"+category+"','"+user+"','"+type+"','"+subfield+"','"+approve+"','"+like+"')";
        DataBase.init();
        int flag= DataBase.operate(sql);
        LabelDao labelDao=new LabelDaoImpl();
        if(flag>0&&labelDao.addLabel(article.getLabel(),DataBase.getId())){
            return true;
        }
        return false;
    }

    @Override
    public ArrayList<Article> getArticles(User user) throws SQLException {
        ArrayList<Article> articleArrayList = new ArrayList<>();
        LabelDao labelDao=new LabelDaoImpl();
        CommentDao commentDao=new CommentDaoImpl();
        String sql="select * from article_table where article_user='"+user.getAccount()+"'and article_approve=1";
        DataBase.init();
        ResultSet resultSet= DataBase.inquire(sql);
        while(resultSet.next()){
            int id=resultSet.getInt("article_id");
            String content=resultSet.getString("article_content");
            int likingDegree=resultSet.getInt("article_likingDegree");
            int views=resultSet.getInt("article_views");
            String title=resultSet.getString("article_title");
            String time = resultSet.getString("article_time");
            int commentCount=resultSet.getInt("article_commentCount");
            int category=resultSet.getInt("article_categoryId");
            String userAccount=resultSet.getString("article_user");
            String type=resultSet.getString("article_type");
            String subfield=resultSet.getString("article_subfield");
            int approve=resultSet.getInt("article_approve");
            int like=resultSet.getInt("article_like");
            Article article=new Article(id,content,likingDegree,views,title,time,commentCount,category,userAccount,labelDao.getLabels(id),commentDao.getComments(id),type,subfield,approve,like);
            articleArrayList.add(article);
        }
        return articleArrayList;
    }

    @Override
    public boolean deleteArticle(int articleId) throws SQLException {
        String sql="delete from article_table where article_id='"+articleId+"'";
        DataBase.init();
        int flag=DataBase.operate(sql);/*删除博客信息*/
        DataBase.release();
        LabelDao labelDao=new LabelDaoImpl();
        if(flag>0&&labelDao.deleteArticleLabel(articleId)){/*删除标签*/
            return true;
        }
        return false;
    }

    @Override
    public boolean updateArticle(Article article) throws SQLException {
        int id=article.getId();
        String content=article.getContent();
        int likingDegree=article.getLikingDegree();
        int views=article.getViews();
        String title=article.getTitle();
        String time=article.getTime();
        int commentCount=article.getCommentCount();
        int category=article.getCategory();
        String type=article.getType();
        String subfield=article.getSubfield();
        int approve=article.getApprove();
        int like=article.getLike();
        String sql="update article_table set article_content= '"+content+"'" +
                ",article_time='"+time+"',article_categoryId='"+category+"',article_title='"+ title+"'," +
                "article_type='"+type+"',article_commentCount='"+commentCount+"',article_likingDegree='"+likingDegree
                +"',article_views='"+views+"',article_approve='"+approve+"',article_like='"+like+"',article_subfield='"+subfield+"' where article_id='"+id+"'";
        DataBase.init();
        int flag=DataBase.operate(sql);
        DataBase.release();
        if(flag>0){
            return true;
        }
        return false;
    }



    @Override
    public Article getArticle(int articleId) throws SQLException {
        Article article=null;
        LabelDao labelDao=new LabelDaoImpl();
        CommentDao commentDao=new CommentDaoImpl();
        String sql="select * from article_table where article_id='"+articleId+"' and article_approve= 1";
        DataBase.init();
        ResultSet resultSet=DataBase.inquire(sql);
        while (resultSet.next()) {
            String content = resultSet.getString("article_content");
            int likingDegree = resultSet.getInt("article_likingDegree");
            int views = resultSet.getInt("article_views");
            String title = resultSet.getString("article_title");
            String time = resultSet.getString("article_time");
            int commentCount = resultSet.getInt("article_commentCount");
            int category = resultSet.getInt("article_categoryId");
            String userAccount = resultSet.getString("article_user");
            int id = resultSet.getInt("article_id");
            String type = resultSet.getString("article_type");
            String subfield=resultSet.getString("article_subfield");
            int approve=resultSet.getInt("article_approve");
            int like=resultSet.getInt("article_like");
            article=new Article(id,content,likingDegree,views,title,time,commentCount,category,userAccount,labelDao.getLabels(id),commentDao.getComments(id),type,subfield,approve,like);
        }
        DataBase.release();
        return article;
    }

    @Override
    public Map<String, ArrayList<Article>> getAllMapArticles() throws SQLException {
        UserDao userDao=new UserDaoImpl();
        ArticleDao articleDao=new ArticleDaoImpl();
        Map<String,ArrayList<Article>> articles=new HashMap<>();
        ArrayList<User> users=userDao.getAllUsers();
        for(int i=0;i<users.size();i++){
            articles.put(users.get(i).getAccount(),articleDao.getArticles(users.get(i)));
        }
        return articles;
    }

    @Override
    public ArrayList<Article> getAllArticles() throws SQLException {
        ArrayList<Article> articles=new ArrayList<>();
        String sql="select * from article_table where article_approve = 1";
        DataBase.init();
        ResultSet resultSet=DataBase.inquire(sql);
        LabelDao labelDao=new LabelDaoImpl();
        CommentDao commentDao=new CommentDaoImpl();
        while (resultSet.next()){
            String content = resultSet.getString("article_content");
            int likingDegree = resultSet.getInt("article_likingDegree");
            int views = resultSet.getInt("article_views");
            String title = resultSet.getString("article_title");
            String time = resultSet.getString("article_time");
            int commentCount = resultSet.getInt("article_commentCount");
            int category = resultSet.getInt("article_categoryId");
            String userAccount = resultSet.getString("article_user");
            int id = resultSet.getInt("article_id");
            String type = resultSet.getString("article_type");
            String subfield=resultSet.getString("article_subfield");
            int approve=resultSet.getInt("article_approve");
            int like=resultSet.getInt("article_like");
            articles.add(new Article(id,content,likingDegree,views,title,time,commentCount,category,userAccount,labelDao.getLabels(id),commentDao.getComments(id),type,subfield,approve,like));
        }
        DataBase.release();
        return articles;
    }

//    @Override
//    public ArrayList<Article> getSearchResultList(String content) throws SQLException {
//        ArrayList<Article> articles=new ArrayList<>();
//        content="%"+content+"%";
//        String sql="select * from article_table where article_content like '%"+content+"%' or article_title like '%"+content+"%' or " +
//                "article_type like '%"+content+"%' or article_subfield like '%"+content+"%' or article_user like '%"+content+"%'";
//        DataBase.init();
//        ResultSet resultSet=DataBase.inquire(sql);
//        while(resultSet.next()){
//            String content2 = resultSet.getString("article_content");
//            int likingDegree = resultSet.getInt("article_likingDegree");
//            int views = resultSet.getInt("article_views");
//            String title = resultSet.getString("article_title");
//            String time = resultSet.getString("article_time");
//            int commentCount = resultSet.getInt("article_commentCount");
//            String category = resultSet.getString("article_category");
//            String userAccount = resultSet.getString("article_user");
//            String account = resultSet.getString("article_account");
//            String label = resultSet.getString("article_label");
//            String type = resultSet.getString("article_type");
//            String subfield=resultSet.getString("article_subfield");
//            Article article = new Article(account, content2, likingDegree, views, title, time, commentCount, category, userAccount, label, type,subfield);
//            articles.add(article);
//        }
//        DataBase.release();
//        return articles;
//    }


//    @Override
//    public ArrayList<Article> getAllBlog() throws SQLException {
//        ArrayList<Article> articles=new ArrayList<>();
//        String sql="select * from article_table";
//        DataBase.init();
//        ResultSet resultSet=DataBase.inquire(sql);
//        while (resultSet.next()){
//            String content2 = resultSet.getString("article_content");
//            int likingDegree = resultSet.getInt("article_likingDegree");
//            int views = resultSet.getInt("article_views");
//            String title = resultSet.getString("article_title");
//            String time = resultSet.getString("article_time");
//            int commentCount = resultSet.getInt("article_commentCount");
//            String category = resultSet.getString("article_category");
//            String userAccount = resultSet.getString("article_user");
//            String account = resultSet.getString("article_account");
//            String label = resultSet.getString("article_label");
//            String type = resultSet.getString("article_type");
//            String subfield=resultSet.getString("article_subfield");
//            Article article = new Article(account, content2, likingDegree, views, title, time, commentCount, category, userAccount, label, type,subfield);
//            articles.add(article);
//        }
//        DataBase.release();
//        return articles;
//    }

    @Override
    public ArrayList<Article> getCollectedArticles(User user) throws SQLException {
        CollectionDao collectionDao=new CollectionDaoImpl();
        LabelDao labelDao=new LabelDaoImpl();
        CommentDao commentDao=new CommentDaoImpl();
        ArrayList<Article> articles=new ArrayList<>();
        ArrayList<Collection> collections=collectionDao.getCollections(user);
        for(int i=0;i<collections.size();i++){
            DataBase.init();
            String sql="select * from article_table where article_id='"+collections.get(i).getArticleId()+"' and article_approve= 1";
            ResultSet resultSet=DataBase.inquire(sql);
            while (resultSet.next()) {
                String content = resultSet.getString("article_content");
                int likingDegree = resultSet.getInt("article_likingDegree");
                int views = resultSet.getInt("article_views");
                String title = resultSet.getString("article_title");
                String time = resultSet.getString("article_time");
                int commentCount = resultSet.getInt("article_commentCount");
                int category = resultSet.getInt("article_categoryId");
                String userAccount = resultSet.getString("article_user");
                int id = resultSet.getInt("article_id");
                String type = resultSet.getString("article_type");
                String subfield=resultSet.getString("article_subfield");
                int approve=resultSet.getInt("article_approve");
                int like=resultSet.getInt("article_like");
                articles.add(new Article(id,content,likingDegree,views,title,time,commentCount,category,userAccount,labelDao.getLabels(id),commentDao.getComments(id),type,subfield,approve,like));
            }
            DataBase.release();
        }
        return articles;
    }

    @Override
    public boolean recordArticle(int articleId, int views, int commentCount, int likeDegree, int like) {
        String sql="update article_table set article_views='"+views+"',article_commentCount='"+commentCount+"',article_likeDegree='"+likeDegree+"',article_like='"+like+"'";
        DataBase.init();
        int flag=DataBase.operate(sql);
        DataBase.release();
        if(flag>0){
            return true;
        }
        return false;
    }

    @Override
    public ArrayList<Article> getCommandArticle() throws SQLException {
        CommandDao commandDao=new CommandDaoImpl();
        ArticleDao articleDao=new ArticleDaoImpl();
        ArrayList<Command> commands=commandDao.getAllCommand();
        ArrayList<Article> articles=new ArrayList<>();
        for(int i=0;i<commands.size();i++){
            articles.add(articleDao.getArticle(commands.get(i).getArticleId()));
        }
        return articles;
    }

    @Override
    public ArrayList<Article> getLikedArticles(User user) throws SQLException {
        LikeDao likeDao=new LikeDaoImpl();
        LabelDao labelDao=new LabelDaoImpl();
        CommentDao commentDao=new CommentDaoImpl();
        ArrayList<Like> likes=likeDao.getLikes(user);
        ArrayList<Article> articles=new ArrayList<>();
        for(Like like:likes){
            DataBase.init();
            String sql="select * from article_table where article_id='"+like.getArticleId()+"' and article_approve= 1";
            ResultSet resultSet=DataBase.inquire(sql);
            while (resultSet.next()) {
                String content = resultSet.getString("article_content");
                int likingDegree = resultSet.getInt("article_likingDegree");
                int views = resultSet.getInt("article_views");
                String title = resultSet.getString("article_title");
                String time = resultSet.getString("article_time");
                int commentCount = resultSet.getInt("article_commentCount");
                int category = resultSet.getInt("article_categoryId");
                String userAccount = resultSet.getString("article_user");
                int id = resultSet.getInt("article_id");
                String type = resultSet.getString("article_type");
                String subfield=resultSet.getString("article_subfield");
                int approve=resultSet.getInt("article_approve");
                int likes1=resultSet.getInt("article_like");
                articles.add(new Article(id,content,likingDegree,views,title,time,commentCount,category,userAccount,labelDao.getLabels(id),commentDao.getComments(id),type,subfield,approve,likes1));
            }
            DataBase.release();
        }
        return articles;
    }

    @Override
    public ArrayList<Article> getCategoriesArticles(int categoryId) throws SQLException {
      ArrayList<Article> articles=new ArrayList<>();
      DataBase.init();
      LabelDao labelDao=new LabelDaoImpl();
      CommentDao commentDao=new CommentDaoImpl();
      String sql="select * from article_table where article_categoryId='"+categoryId+"'and article_approve=1";
      ResultSet resultSet=DataBase.inquire(sql);
      while (resultSet.next()){
          String content = resultSet.getString("article_content");
          int likingDegree = resultSet.getInt("article_likingDegree");
          int views = resultSet.getInt("article_views");
          String title = resultSet.getString("article_title");
          String time = resultSet.getString("article_time");
          int commentCount = resultSet.getInt("article_commentCount");
          int category = resultSet.getInt("article_categoryId");
          String userAccount = resultSet.getString("article_user");
          int id = resultSet.getInt("article_id");
          String type = resultSet.getString("article_type");
          String subfield=resultSet.getString("article_subfield");
          int approve=resultSet.getInt("article_approve");
          int like=resultSet.getInt("article_id");
          articles.add(new Article(id,content,likingDegree,views,title,time,commentCount,category,userAccount,labelDao.getLabels(id),commentDao.getComments(id),type,subfield,approve,like));
      }
      DataBase.release();
      return articles;
    }

    @Override
    public boolean updateDefaultCategory(ArrayList<Article> articles, int defaultCategoryId) {
        if(articles.size()==0){
            return true;
        }else {
            for (Article article : articles) {
                String sql = "update article_table set article_categoryId='" + defaultCategoryId + "'where article_Id='" + article.getId() + "'";
                DataBase.init();
                int flag = DataBase.operate(sql);
                if (flag > 0) {
                    return true;
                }
            }
            return false;
        }
    }
    @Override
    public ArrayList<Article> getPaginateArticle(int page, int cnt, User user) throws SQLException {
        ArrayList<Article> articles=new ArrayList<>();
        ArrayList<Collection> collections=new ArrayList<>();
        LabelDao labelDao=new LabelDaoImpl();
        CommentDao commentDao=new CommentDaoImpl();
        String sql="select * from collection_table where collection_user='"+user.getAccount()+"' order by collection_id"+" limit "+page+","+cnt;
        System.out.println(sql);
        DataBase.init();
        ResultSet resultSet=DataBase.inquire(sql);
        while (resultSet.next()) {
            int collectionId=resultSet.getInt("collection_id");
            int articleId=resultSet.getInt("collection_articleId");
            String userId=resultSet.getString("collection_user");
            collections.add(new Collection(collectionId,userId,articleId));
        }
        DataBase.release();
        for(int i=0;i<collections.size();i++) {
            DataBase.init();
            sql = "select * from article_table where article_id='" + collections.get(i).getArticleId() + "' and article_approve=1";
            ResultSet resultSet1 = DataBase.inquire(sql);
            while (resultSet1.next()) {
                String content = resultSet1.getString("article_content");
                int likingDegree = resultSet1.getInt("article_likingDegree");
                int views = resultSet1.getInt("article_views");
                String title = resultSet1.getString("article_title");
                String time = resultSet1.getString("article_time");
                int commentCount = resultSet1.getInt("article_commentCount");
                int category = resultSet1.getInt("article_categoryId");
                String userAccount = resultSet1.getString("article_user");
                int id = resultSet1.getInt("article_id");
                String type = resultSet1.getString("article_type");
                String subfield=resultSet1.getString("article_subfield");
                int approve=resultSet1.getInt("article_approve");
                int likes1=resultSet1.getInt("article_like");
                articles.add(new Article(id, content, likingDegree, views, title, time, commentCount, category, userAccount, labelDao.getLabels(id), commentDao.getComments(id), type, subfield, approve, likes1));
            }
            DataBase.release();
        }
        return articles;
    }

    @Override
    public int getPage(User user) throws SQLException {
        String sql="select * from collection_table where collection_user='"+user.getAccount()+"'";
        DataBase.init();
        ResultSet resultSet=DataBase.inquire(sql);
        int cnt=0;
        while (resultSet.next()){
            cnt++;
        }
        DataBase.release();
        if(cnt%3==0){
            return cnt/3;
        }else{
            return cnt/3+1;
        }
    }

    @Override
    public ArrayList<Article> getArticles(String account) throws SQLException {
        ArrayList<Article> articleArrayList = new ArrayList<>();
        LabelDao labelDao=new LabelDaoImpl();
        CommentDao commentDao=new CommentDaoImpl();
        DataBase.init();
        String sql="select * from article_table where article_user='"+account+"'and article_approve=1";
        ResultSet resultSet= DataBase.inquire(sql);
        while(resultSet.next()){
            int id=resultSet.getInt("article_id");
            String content=resultSet.getString("article_content");
            int likingDegree=resultSet.getInt("article_likingDegree");
            int views=resultSet.getInt("article_views");
            String title=resultSet.getString("article_title");
            String time = resultSet.getString("article_time");
            int commentCount=resultSet.getInt("article_commentCount");
            int category=resultSet.getInt("article_categoryId");
            String userAccount=resultSet.getString("article_user");
            String type=resultSet.getString("article_type");
            String subfield=resultSet.getString("article_subfield");
            int approve=resultSet.getInt("article_approve");
            int like=resultSet.getInt("article_like");
            articleArrayList.add(new Article(id,content,likingDegree,views,title,time,commentCount,category,userAccount,labelDao.getLabels(id),commentDao.getComments(id),type,subfield,approve,like));
        }
        DataBase.release();
        return articleArrayList;
    }
}
